package ai.mspbots.test.mapper;

import ai.mspbots.test.dto.AzureAIConfig;
import ai.mspbots.test.entity.TenantIntegration;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface TenantIntegrationMapper extends BaseMapper<TenantIntegration> {

    /**
     * 根据租户代码查询集成配置
     */
    @Select("SELECT * FROM tenant_integration WHERE tenant_code = #{tenantCode}")
    List<TenantIntegration> selectByTenantCode(@Param("tenantCode") Long tenantCode);

    /**
     * 根据集成ID查询配置
     */
    @Select("SELECT * FROM tenant_integration WHERE integration_id = #{integrationId}")
    List<TenantIntegration> selectByIntegrationId(@Param("integrationId") Long integrationId);

    /**
     * 根据租户和集成ID查询唯一配置
     */
    @Select("SELECT * FROM tenant_integration WHERE tenant_code = #{tenantCode} AND integration_id = #{integrationId}")
    TenantIntegration selectByTenantAndIntegration(@Param("tenantCode") Long tenantCode,
                                                  @Param("integrationId") Long integrationId);

    /**
     * 检查租户集成配置是否存在
     */
    @Select("SELECT COUNT(*) FROM tenant_integration WHERE tenant_code = #{tenantCode} AND integration_id = #{integrationId}")
    int existsByTenantAndIntegration(@Param("tenantCode") Long tenantCode,
                                    @Param("integrationId") Long integrationId);

    /**
     * 查询特定状态的集成配置
     */
    @Select("SELECT * FROM tenant_integration WHERE status = #{status}")
    List<TenantIntegration> selectByStatus(@Param("status") Integer status);

    /**
     * 查询JSON参数中包含特定值的配置
     */
    @Select("""
        SELECT * FROM tenant_integration
        WHERE params_values::jsonb @> #{jsonValue}::jsonb
        """)
    List<TenantIntegration> selectByJsonValue(@Param("jsonValue") String jsonValue);

    List<AzureAIConfig> getByIntegrations(Long integrationId);
}